package com.ruoyi.system.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Video;
import com.ruoyi.system.service.IVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 视频Controller
 *
 * @author ...
 * @date 2022-03-17
 */
@RestController
@RequestMapping("/system/video")
public class VideoController extends BaseController {
    @Autowired
    private IVideoService videoService;

    /**
     * 查询视频列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Video video) {
        startPage();
        List<Video> list = videoService.selectVideoList(video);
        return getDataTable(list);
    }

    /**
     * 导出视频列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, Video video) {
        List<Video> list = videoService.selectVideoList(video);
        ExcelUtil<Video> util = new ExcelUtil<Video>(Video.class);
        util.exportExcel(response, list, "视频数据");
    }

    /**
     * 获取视频详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(videoService.selectVideoById(id));
    }

    /**
     * 新增视频
     */
    @PostMapping
    public AjaxResult add(@RequestBody Video video) {
        video.setCreateUser(SecurityUtils.getUserId().intValue());
        video.setCreateTime(DateUtils.getNowDate());
        return toAjax(videoService.insertVideo(video));
    }

    /**
     * 修改视频
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Video video) {
        video.setUpdateUser(SecurityUtils.getUserId().intValue());
        video.setUpdateTime(DateUtils.getNowDate());
        return toAjax(videoService.updateVideo(video));
    }

    /**
     * 删除视频
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(videoService.deleteVideoByIds(ids));
    }
}
